package com.beagle.njddapp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.MessageBean;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hxx
 * @date 2021-03-19 18:46
 * 消息adapter
 */
public class NjMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MessageBean> messageBeanList;
    private Context context;
    private RecyclerViewItemClickLisenter<MessageBean> onItemClickLisenter;

    public void setOnItemClickLisenter(RecyclerViewItemClickLisenter<MessageBean> onItemClickLisenter) {
        this.onItemClickLisenter = onItemClickLisenter;
    }

    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MessageBean messageBean= (MessageBean) view.getTag();
            onItemClickLisenter.onItemClickLisenter(messageBean);
        }
    };

    public NjMessageAdapter(List<MessageBean> messageBeanList, Context context) {
        this.messageBeanList = messageBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_message,null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyHolder myHolder= (MyHolder) holder;
        MessageBean messageBean=messageBeanList.get(position);
        myHolder.tvDesc.setText(messageBean.getDesc());
        myHolder.tvTime.setText(messageBean.getRecieveTime());
        myHolder.tvTitle.setText(messageBean.getTitle());
        if ("common".equals(messageBean.getMessageType())){
            myHolder.ivCover.setImageResource(R.mipmap.message_ic_orderstatus);

        }else {
            myHolder.ivCover.setImageResource(R.mipmap.message_ic_refuelingorder);
        }
        if (messageBean.isReaded()){
            myHolder.readview.setVisibility(View.GONE);
        }else {
            myHolder.readview.setVisibility(View.VISIBLE);
        }
        myHolder.itemView.setTag(messageBean);
        myHolder.itemView.setOnClickListener(onClickListener);

    }

    @Override
    public int getItemCount() {
        return messageBeanList.size();
    }
    public class MyHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.message_iv)
        ImageView ivCover;
        @BindView(R.id.title_tv)
        TextView tvTitle;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_desc)
        TextView tvDesc;
        @BindView(R.id.read_view)
        View readview;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
